
<template>
  <div class="login-container">
    <el-row :gutter="10" class="login-row">
      <el-col :xs="24" :sm="24" :md="24" :lg="12" :xl="12" class="pd0">
        <div class="login-left flex-margin">
          <img src="../../../assets/login/login-banner.png" />
        </div>
      </el-col>
      <el-col :xs="24" :sm="24" :md="24" :lg="12" :xl="12" class="pd0">
        <div class="login-right flex">
          <div class="login-right-title">{{systemName }}</div>
          <div class="login-right-warp flex-margin">
            <div class="login-right-warp-main">
              <div class="login-right-warp-main-title">
                <div class="login-right-warp-main-title-sub">
                  {{ hospitalName }}
                </div>
              </div>
              <div class="login-right-warp-main-form">
                <!-- 用户名和手机号登陆 -->
                <div class="login-tab">
                  <el-form
                    ref="form"
                    :model="model"
                    :rules="rules"
                    hide-required-asterisk
                  >
                    <el-form-item prop="username">
                      <el-input v-model="model.username" style="height: 42px"   prefix-icon="User" placeholder="请输入用户名"/>
                    </el-form-item>

                    <el-form-item prop="password">
                      <el-input
                        v-model="model.password"
                        style="height: 42px"
                        :type="state.isShowPassword ? 'text' : 'password'"
                        show-password
                        prefix-icon="Unlock"
                        placeholder="请输入密码"
                        @keyup.enter="handleLogin"
                      />
                    </el-form-item>

                    <el-form-item>
                      <el-button
                        style="width: 100%; height: 42px"
                        type="primary"
                        :loading="loading"
                        @click="handleLogin"
                      >
                        登录
                      </el-button>
                    </el-form-item>
                  </el-form>
                  <!-- <span v-if="!band" @click="open" class="login-band">绑定</span> -->
                </div>
              </div>
            </div>
          </div>
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import Captcha from "./components/captcha/index.vue";
import { loginAPI } from "@/api/login.js";
import http from "@/utils/request";


export default {
  name: "Login",

  components: {
    Captcha,
  },

  data() {
    return {
      band: false,
      state: {
        tabsActiveName: "account",
        isShowPassword: false,
      },
      systemName:"",
      hospitalName:"",
      showDate: "",
      userInfo:{},
      // 表单数据对象
      model: {
        username: "",
        password: "",
      
        // captchaId: "", // 验证码id
        // verifyCode: "",
      },
      // 表单校验规则
      rules: {
        username: [{ required: true, message: "请输入用户名" }],
        password: [{ required: true, message: "请输入密码" }],
        // verifyCode: [{ required: true, message: "VerifyCode 必须" }],
      },
      loading: false, // 用于表示当前是否请求中
    };
  },

  methods: {
    //处理绑定
    // open() {

    //     this.$prompt('请输入IP和MAC地址，如：192.168.1.2,ab:f0:22:9f:ge', '提示', {
    //       confirmButtonText: '确定',
    //       cancelButtonText: '取消',
    //     }).then(({ value }) => {
    //       if (value) {
    //         localStorage.setItem('band', value)
    //         this.band = value
    //         this.$message({
    //           type: 'success',
    //           message: '绑定成功'
    //         })
    //         this.handleLogin();
    //       } else {
    //         return false
    //       }
    //     }).catch(() => {
    //       this.$message({
    //         type: 'info',
    //         message: '取消输入'
    //       })
    //     })
    //   },
    /**
     * 处理登录
     */
    async handleLogin() {
      // if (this.loading) return;

      // 1. 手动表单校验，回调函数，回调函数的参数是一个boolean
      //                Promise
      
      // if (!localStorage.getItem('band')) {
      //   this.$message({
      //     type: 'warning',
      //     message: '请绑定IP,MAC地址'
      //   })
      //   return false
      // }
      await this.$refs.form.validate();
      try {
        // 2. 校验成功，直接去登录
        //    设置 loading 为 true
        this.loading = true;
        const result = await loginAPI(this.model);

        // 3. 登录成功
        // alert("登录成功");
        this.loading = false;
        // 4. 将 result.token 写入到 vuex 中
        this.$store.commit("user/setToken", result.data.token);
        console.log(this.userInfo);
        localStorage.setItem("userInfo",JSON.stringify(this.userInfo));
        localStorage.setItem("isLoggedIn", true);
        sessionStorage.setItem("isLoggedIn", true);
        sessionStorage.setItem("isPageRefreshed", "true");
      //  console.log(result.data.token);
       
        

        // 5. 跳转回之前要去的页面
        // const redirect = this.$route.query.redirect || "/";
         const redirect =  "/";
        this.$router.replace(redirect);
      } catch (error) {
        // 登录失败，刷新验证码
        // this.$refs.captchaRef.refresh();
        // this.model.verifyCode = "";
        this.loading = false;
      }
    },
  },
  created() {
    http.get("/admin/index/getName").then(res => {
      if(res.code == 200){
        this.systemName = res.data.systemName;
        this.hospitalName = res.data.hospitalName;
        this.userInfo.disease = res.data.disease;
        this.userInfo.examination = res.data.examination;
        this.userInfo.record = res.data.record;
      }
    });
  },
  mounted() {
    this.band = localStorage.getItem('band') || ''
  },
};
</script>

<style scoped lang="scss">
.login-container {
  height: 100%;
  background: var(--el-color-white);

  .login-row {
    height: 100%;
    width: 100%;
    margin: 0 !important;

    .pd0 {
      padding: 0 !important;
    }
  }
  .login-left {
    height: 100%;
    img {
      width: 100%;
      height: 100%;
    }
  }
  .login-right {
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: #c3e2ff;
    flex-direction: column;
    .login-right-title {
      font-size: 35px;
      font-weight: 900;
      text-align: center;
      letter-spacing: 3px;
      animation: logoAnimation 0.3s ease;
      animation-delay: 0.3s;
      color: #ffffff;

      padding: 30px 0;
      margin-bottom: 15px;
    }
    .login-right-warp {
      // border: 1px solid var(--el-color-primary-light-3);
      border-radius: 3px;
      width: 450px;
      position: relative;
      overflow: hidden;
      background-color: var(--el-color-white);

      .login-right-warp-main {
        display: flex;
        flex-direction: column;
        height: 100%;
        .login-right-warp-main-title {
          font-size: 26px;
          font-weight: 400;
          text-align: center;
          letter-spacing: 3px;
          animation: logoAnimation 0.3s ease;
          animation-delay: 0.3s;
          color: #000000;
          padding: 20px 0;

          img {
            width: 100px;
            height: 90px;
          }
          .login-right-warp-main-title-sub {
            font-size: 20px;
            font-weight: 400;
            text-align: center;
            letter-spacing: 3px;
            animation: logoAnimation 0.3s ease;
            animation-delay: 0.3s;
            color: #409eff;

            padding: 25px 0;
          }
        }
        .login-right-warp-main-form {
          flex: 1;
          padding: 0 50px 50px 50px;
          .login-content-main-scan {
            position: absolute;
            top: 0;
            right: 0;
            width: 50px;
            height: 50px;
            overflow: hidden;
            cursor: pointer;
            transition: all ease 0.3s;
            color: var(--el-color-primary);
            &-delta {
              position: absolute;
              width: 35px;
              height: 70px;
              z-index: 2;
              top: 2px;
              right: 21px;
              background: var(--el-color-white);
              transform: rotate(-45deg);
            }
            &:hover {
              opacity: 1;
              transition: all ease 0.3s;
              color: var(--el-color-primary) !important;
            }
            i {
              width: 47px;
              height: 50px;
              display: inline-block;
              font-size: 48px;
              position: absolute;
              right: 1px;
              top: 0px;
            }
           
            
         
            
          }
        }
      }
    }
  }
}
.login-band{
              font-size: 16px;
              color: #409eff;
              cursor: pointer;
            }
.change-tabs {
  :deep(.el-tabs__nav) {
    width: 100%;

    .el-tabs__active-bar {
      background-color: #0064fb;
    }
    .el-tabs__item {
      width: 50%;
      font-size: 16px;
    }
    .el-tabs__item:hover {
      color: #0064fb;
    }

    .is-active {
      color: #0064fb;
    }
  }
}

// 注册
.register {
  :deep(.el-form-item__content) {
    .is-focus {
      box-shadow: 0 0 0 1px #016dff inset;
    }

    .login-content-code {
      color: #ffffff;
      border-color: #016dff;
      background-color: #016dff;
    }

    .el-button:focus,
    .el-button:hover {
      color: #ffffff;
      border-color: #016dff;
      background-color: #016dff;
    }

    .href {
      display: inline-block;
      width: 100%;
      height: 100%;
      text-align: left;
      line-height: 40px;
      color: #016dff;
      font-size: 14px;
      text-decoration: none;
    }

    .href:hover {
      text-decoration: none;
      --el-link-hover-text-color: #ffffff;
    }
  }
}
.login-content-form {
  margin-top: 20px;
  @for $i from 1 through 4 {
    .login-animation#{$i} {
      opacity: 0;
      animation-name: error-num;
      animation-duration: 0.5s;
      animation-fill-mode: forwards;
      animation-delay: calc($i/10) + s;
    }
  }
  .login-content-code {
    width: 100%;
    padding: 0;
  }

  .login-msg {
    color: var(--el-text-color-placeholder);
  }
}

.login-content-submit {
  width: 100%;
  letter-spacing: 2px;
  font-weight: 300;
  margin-top: 15px;
  background: #016dff;
  border: none;
  border-radius: 6px;
  height: 44px;
  font-size: 16px;
}

.register-btn {
  display: inline-block;
  // width: 100%;
  // height: 100%;
  line-height: 40px;
  color: #016dff;
  font-size: 14px;
  text-decoration: none;
  // background: #016dff;
  border-radius: 6px;
  // margin-top: 30px;
}

.register-btn:hover {
  text-decoration: none;
  --el-link-hover-text-color: #ffffff;
  border: none;
}

.login-block {
  :deep(.el-checkbox) {
    .el-checkbox__input.is-checked .el-checkbox__inner {
      border-color: #016dff;
      background: #016dff;
    }

    .el-checkbox__label {
      color: #016dff;
      font-size: 14px;
    }

    .el-checkbox__inner:hover {
      border-color: #016dff;
    }
  }

  .link-forget {
    display: inline-block;
    // width: 100%;
    // height: 100%;
    line-height: 40px;
    color: #016dff;
    font-size: 14px;
    text-decoration: none;
    margin-right: 10px;
  }

  .link-forget:hover {
    text-decoration: none;
    --el-link-hover-text-color: #ffffff;
  }
}
</style>
